/*
 * MainActivity serves as the hub for users to manage buildings & inventory. From this page, the user will
 * be directed to various sub activities to complete operations such as creating, updating, & deleting buildings 
 * & inventory, & managing the application.
 */
package com.example.wmuqrreader;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
	
	DatabaseHandler db;
	
	ListView list;
	
	Button button_new;
	Button button_scan;
	
	// OnCreate is auto-generated & is always the first method to be called by a new activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);		//Link this activity to a specific view
        
        button_new 		= (Button) findViewById(R.id.btn_new);   
		button_scan 	= (Button) findViewById(R.id.btn_scan);
		
		db 				= new DatabaseHandler(this);
		list 			= (ListView) findViewById(R.id.listView1);
		
        List<Building> building = new ArrayList<Building>();
        building = db.getAllBuilding();

        CustomBuildingListViewAdapter adapter = new CustomBuildingListViewAdapter(this, R.layout.simple_list_building, building);
        
        //Add a function for the EDIT row click
        list.setAdapter(adapter);       
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
            	Intent intent = new Intent();
            	
            	//Fetch the ID of the entry we just grabbed from the ListView & set as extra param to pass to next view
            	
            	View rowItem 	= (View) av.getChildAt(pos);
            	Toast.makeText(getApplicationContext(), (String) ((TextView) rowItem.findViewById(R.id.textView_id)).getText(), Toast.LENGTH_LONG).show();
            	intent.putExtra("BUILDING", (String) ((TextView) rowItem.findViewById(R.id.textView_num)).getText());
				
				//intent.setClass(MainActivity.this, InvActivity.class);
				
				//TODO: specific function for this result?
		        //startActivityForResult(intent, 0);
            }
        });
           
        //Add a function for the NEW button click          
        button_new.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				
				//TODO: [Ryan] Un-comment & edit this code to redirect the user to the activity_building view
				//The necessary code should all be here - you just need to change the values & understand how it works :)
				/*
				intent.setClass(InvList.this, WideActivity.class);
		        startActivityForResult(intent, 1);	
		        */	        
			}
		});
        
        //Add a function for the SCAN button click
        button_scan.setOnClickListener(new View.OnClickListener() {
                        
                @Override
                public void onClick(View v) {
                        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                        intent.setPackage("com.google.zxing.client.android");
                        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                        startActivityForResult(intent, 0);                     
                }
         });
        
        
    }  


    //TODO: Move to DatabaseHandler?
    //Populate the building list if empty. 
    private void populateList(boolean b) {
    	//TODO: No checks currently exist to ensure list is empty. Add checks
    	//TODO: Populate using csv file
		if(b){
			db.addBuilding(new Building(1, "EAST HALL"));
			db.addBuilding(new Building(2, "POWER PLANT"));
			db.addBuilding(new Building(3, "NORTH HALL"));
			db.addBuilding(new Building(4, "WEST HALL"));
			db.addBuilding(new Building(5, "SPEECH AND HEARING CENTER"));
			db.addBuilding(new Building(6, "SEEYLE CENTER"));
			db.addBuilding(new Building(7, "VANDERCOOK HALL"));
			db.addBuilding(new Building(8, "SPINDLER HALL"));
			db.addBuilding(new Building(9, "OAKLAND RECITAL"));
			db.addBuilding(new Building(10, "WALWOOD HALL"));
			db.addBuilding(new Building(11, "UPHOLSTERY HALL"));
			db.addBuilding(new Building(13, "ANTENNA BUILDING"));
			db.addBuilding(new Building(15, "WALDO STADIUM/ BILL BROWN"));
			db.addBuilding(new Building(17, "UNIFIED CLINIC"));
			db.addBuilding(new Building(19, "MONTAGUE HOUSE"));
			db.addBuilding(new Building(21, "PHYSICAL PLANT"));
			db.addBuilding(new Building(22, "CAMPUS SERVICES"));
			db.addBuilding(new Building(24, "LEE HONORS COLLEGE"));
			db.addBuilding(new Building(27, "MCCRACKEN HALL"));
			db.addBuilding(new Building(28, "BURNHAM HALLS"));
			db.addBuilding(new Building(29, "UNIVERSITY COMPUTING CENTER"));
			db.addBuilding(new Building(30, "DRAPER-SEIDSCHLAG HALLS"));
			db.addBuilding(new Building(31, "KANLEY CHAPEL"));
			db.addBuilding(new Building(32, "SEIBERT ADMINISTRATION BUILDING"));
			db.addBuilding(new Building(33, "OAKLANDS"));
			db.addBuilding(new Building(35, "TRIMPE DISTRIBUTIVE EDUCATION"));
			db.addBuilding(new Building(36, "WOOD HALL"));
			db.addBuilding(new Building(37, "ELMWOOD APARTMENTS"));
			db.addBuilding(new Building(40, "MILLER AUDITORIUM"));
			db.addBuilding(new Building(41, "IRVING S. GILMORE COMPLEX"));
			
			db.addInventory(new Inventory("DEFAULT1", 1));
			db.addInventory(new Inventory("DEFAULT2", 2));
		}
		
	}

    // onActivityResult is a callback function that only occurs once a process returns to the activity
    // that called it.
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) 
        {
        	if (resultCode == RESULT_OK) 
            {
        		//Refresh the Inventory List activity to display the updated data     		
        		Intent refresh = new Intent();        		
        		refresh.setClass(this, MainActivity.class);
        		
        		//refresh.putExtra("BUILDING", building_id);
        		
        		startActivity(refresh);
        		this.finish();
            }
        }
        
        if (requestCode == 1 ) 
        {
            if (resultCode == RESULT_OK) 
            {
                String contents = intent.getStringExtra("SCAN_RESULT");
                
                //TODO: Handle successful scan  
                //editText_inv.setText(contents);
                //populateFields(inventory_num);
                
                //finish();
            } 
            else if (resultCode == RESULT_CANCELED) 
            {
                // Handle cancel
            }
        }
    }

    // Auto-generated method to create menu options
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        
        menu.add("Upload Data");
        menu.add("Change Layout");
        menu.add("Populate List").setOnMenuItemClickListener(new OnMenuItemClickListener()
        {
            public boolean onMenuItemClick(MenuItem clickedItem)
            {
            	populateList(true);
            	
            	//TODO: Seek more efficient way to refresh building list
            	Intent intent = new Intent();
            	intent.setClass(MainActivity.this, MainActivity.class);
		        startActivityForResult(intent, 0);
				finish();
				
                return true;
            }
        });
        return true;
    }
    
    
}
